#!/bin/sh
#
# Copyright (c) 2019 Cloudfence - Julio Camargo (JCC)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  1. Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# LEDs
LED1="/dev/led/led1"
LED2="/dev/led/led2"
LED3="/dev/led/led3"

# check if LEDs exists
if [ ! -e "$LED1" ];then
   logger -t "APULED Plugin" "Error: LED device not exists"
fi

check_wan(){
# LED2 - network notifications
# Fetch from dpinger the list of gateways and the status of each one
GWLIST=$(ls /var/run/dpinger* | grep sock)

for GW in $GWLIST; do
    GWSTATUS=$(nc -U $GW | awk '{print $4}')
    # We're ONLINE
    if [ "$GWSTATUS" -eq 0 ];then
        echo "1" > $LED2
    # Some losses - LATENCY
    elif [ "$GWSTATUS" -gt 30 ] && [ "$GWSTATUS" -lt 100 ];then
        LATENCY=1
    # One of our Gateways is DOWN - ALARM
    # Test if the GWs are OK. If just one of the GWs failed the test, the LED will show DOWN status
    elif [ "$GWSTATUS" -eq 100 ];then
        echo "f1" > $LED2
        exit 0
    fi
done

# In case of packets loss, I'll warn!
if [ ! -z "$LATENCY" ];then
    echo "f3" > $LED2
fi
}

check_load(){
# check CPU idle
# LED1 - system notifications
CHK_IDLE=$(vmstat 1 2 | tail -1 | awk '{ print $19 }')

if [ "$CHK_IDLE" -lt "10"  ];then
    echo "f4" > $LED1
else
    echo "1" > $LED1
fi
}

check_disk(){
# check Disk space
# LED3 - system notifications
DSK_FREE=$(df -kh / | tail -1 | awk '{ print 100-$5 }')

if [ "$DSK_FREE" -lt "10"  ];then
    echo "m...---..." > $LED3
else
    echo "1" > $LED3
fi
}

# Main routine
check_load
check_disk
check_wan
